﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinkCard.AdminCP.Common;
using System.Data;
using System.Data.SqlClient;
using LinkCard.Model;

namespace LinkCard.AdminCP.Reports
{
    public partial class export_excel : System.Web.UI.Page
    {
        public string sFromDate;
        public string sToDate;

        public DateTime dFromDate = new DateTime(1900, 1, 1);
        public DateTime dToDate = new DateTime(2099, 1, 1);

       

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["USER"] == null)
            {
                Response.Redirect("/login.aspx");
            }

            if (!Page.IsPostBack)
            {
                if (Request.QueryString["FromDate"] != null) { sFromDate = Request.QueryString["FromDate"].Trim(); }
                if (Request.QueryString["ToDate"] != null) { sToDate = Request.QueryString["ToDate"].Trim(); }

                DataBindPaging();

                Response.Clear();
                Response.Buffer = false;
                Response.AddHeader("content-disposition", "attachment; filename=" + DateTime.Now.ToString("yyyyMMdd") + "Report.xls");
                Response.ContentType = "application/vnd.ms-excel";
                Response.Charset = "UTF-8";
            }

        }

        private void DataBindPaging()
        {
            try
            {
                try
                {
                    var db = new TopLinkEntities();

                    
                    System.Globalization.DateTimeFormatInfo dateInfo = new System.Globalization.DateTimeFormatInfo();
                    dateInfo.ShortDatePattern = "MM/dd/yyyy";

                    if (sFromDate != "")
                    {
                        sFromDate += " 00:00:00";
                        dFromDate = Convert.ToDateTime(sFromDate, dateInfo);

                    }
                    if (sToDate != "")
                    {
                        sToDate += " 23:59:59";
                        dToDate = Convert.ToDateTime(sToDate, dateInfo);
                    }

                    var _paylog = (from payment in db.PayLog
                                   let dt = payment.PayDate
                                   where (payment.PayDate >= dFromDate
                                         && payment.PayDate <= dToDate)
                                   group payment by new { y = dt.Value.Year, m = dt.Value.Month, d = dt.Value.Day } into p
                                   select new
                                   {
                                       PayYear = p.Key.y,
                                       PayMonth = p.Key.m,
                                       PayDay = p.Key.d,
                                       PayAmountSum = p.Sum(s => s.PayAmount),
                                       PayCount = p.Count()
                                   }).OrderBy(t => new { t.PayYear, t.PayMonth, t.PayDay });

                    var paylog = _paylog.ToList();

                    if (paylog.Count > 0)
                    {
                        msgList.DataSource = paylog;
                        msgList.DataBind();
                        msgNoData.Visible = false;
                    }
                    else
                    {
                        msgList.DataSource = null;
                        msgList.DataSourceID = null;
                        msgList.DataBind();
                        msgNoData.Visible = true;
                    }
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
    }
}